## 直接从截图数据文件夹生成配置文件。

import  os
import copy
import xlwt
def gen_ti_string(zhang, jie, xiaojie):
    if zhang=='':
        print('章不能为空！')
        exit()
    str1=''
    if jie=='':
        str1=zhang + '-'
    elif xiaojie=='':
        str1 = zhang + '.' + jie + '-'
    else:
        str1=zhang+'.'+jie+ '.' + xiaojie +'-'
    return str1
def get_tinumber(files, zhang,jie,xiaojie):
    # files=os.listdir(path)
    files_copy=copy.copy(files)
    ti=set()
    str1=gen_ti_string(zhang,jie,xiaojie)
    for f in files_copy:
        if '_' in f:
            continue
        if  f.find(str1)==0:
            n=f.split('-')[1].split('.')[0]
            ti.add(n.zfill(3))
            files.remove(f)
    if ti:
        print('ti=', ti)
        vv=list(ti)
        vv.sort()
        return vv[-1]
    else:
        return 0

#-----------------------------------------
path='E:\高等数学练习册'
files=os.listdir(os.path.join(path, 'data'))

result=[]
for zhang in range(1, 30):
    zhang=str(zhang)
    j=0
    for jie in range(1, 30):
        jie=str(jie)
        i=0
        for xiaojie in range(1, 30):
            xiaojie=str(xiaojie)
            n=get_tinumber(files, zhang,jie,xiaojie)
            if n==0:
                continue
            index1=zhang.zfill(3)+'.'+jie.zfill(3) +'.' + xiaojie.zfill(3)+'-'
            x=(index1, zhang, jie, xiaojie, n)
            result.append(x)
            i=i+1
#---------------------------------
        if i!=0:
            index1 = zhang.zfill(3) + '.' + jie.zfill(3) + '.' + '000' + '-'
            x = (index1, zhang, jie, '', '')
            result.append(x)
        else:
            n = get_tinumber(files, zhang, jie, '')
            if n == 0:
                continue
            index1 = zhang.zfill(3) + '.' + jie.zfill(3) + '-'
            x = (index1, zhang, jie, '', n)
            result.append(x)
            j = j + 1
 #--------------------------------------
    if j!=0:
        index1 = zhang.zfill(3) + '.' + '000' + '.' + '000' + '-'
        x = (index1, zhang, '', '', '')
        result.append(x)
    else:
        n = get_tinumber(files, zhang, '', '')
        if n == 0:
            continue
        index1 = zhang.zfill(3) + '-'
        x = (index1, zhang, '', '', n)
        result.append(x)
result.sort()
for item in result:
     print(item)
# print('result=', result)
xls=xlwt.Workbook()
sht1=xls.add_sheet("Sheet1",cell_overwrite_ok=True)
# for i in range(1,5):
#     for j in range(0,len(result)):
#         ss = str(result[j][i])
#         if ss=='':
#             continue
#         else:
#             ss1 = int(ss)
#             ss2 = str(ss1)
#             sht1.write(j+4,i,ss2)
m=0
for i in range(0,len(result)):
    if str(result[i][2])!='':
        if i-1>=0 and str(result[i-1][2])!='':
            if int(result[i][2])-int(result[i-1][2])!=1:
                m=m+1
    for j in range(1,5):
        ss=str(result[i][j])
        if ss=='':
            continue
        else:
            ss1=int(ss)
            ss2=str(ss1)
            sht1.write(m+4,j,ss2)
    if int(result[i][0][6])==0:
        ss=str(result[i][0][0:3])
        ss1=int(ss)
        ss2=str(ss1)
        sht1.write(m + 4, 0, "第"+ss2+"章")
    else:
        ss=str(result[i][0][4:7])
        ss1 = int(ss)
        ss2=str(ss1)
        sht1.write(m + 4, 0,"第"+ss2+"节")
    m=m+1

# for  i in range(1,5):
#     m=1
#     for j in range(0,len(result)):
#         n=0
#         if j!=len(result)-1:
#             if str(result[j][2])!='' and str(result[j+1][2])!='':
#                 if int(result[j+1][2])-int(result[j][2])!=1:
#                     continue
#         ss = str(result[n][m])
#         print("ss=",ss)
#         if ss=='':
#             continue
#         else:
#             ss1=int(ss)
#             ss2=str(ss1)
#             sht1.write(j+4,i,ss2)
#         m=m+1
#         n=n+1
# for i in range(0,len(result)):
#     if int(result[i][0][6])==0:
#         ss=str(result[i][0][0:3])
#         ss1=int(ss)
#         ss2=str(ss1)
#         sht1.write(i + 4, 0, "第"+ss2+"章")
#     else:
#         ss=str(result[i][0][4:7])
#         ss1 = int(ss)
#         ss2=str(ss1)
#         sht1.write(i + 4, 0,"第"+ss2+"节")
ISBN='9787040196719'
sht1.write(0,0,"书名")
sht1.write(0,1,"书名简称")
sht1.write(0,2,"作者")
sht1.write(0,3,"出版社")
sht1.write(0,4,"版本")
sht1.write(0,5,"封面（文件路径）")
sht1.write(0,6,"ISBN")
sht1.write(0,7,"大学")
sht1.write(0,8,"大学简称")
sht1.write(3,0,"标题")
sht1.write(3,1,"章")
sht1.write(3,2,"节")
sht1.write(3,3,"小节")
sht1.write(3,4,"题的数量（为空表示没有题目）")
sht1.write(1,6,str(ISBN))

place=path+'\\test.xls'
xls.save(place)

